﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SimplePad.Controls;
using ICSharpCode.TextEditor;
using System.IO;
using System.Drawing;

namespace SimplePad
{
    public partial class TextEditor : UserControl
    {
        public TextEditorControl Editor;
        public NotificationBar Notification;
        public int TextHash { get; protected set; }

        public delegate void dOnStatusUpdate();
        public event dOnStatusUpdate OnStatusUpdate;

        public TextEditor(string fileName)
        {
            this.Editor = new TextEditorControl();
            this.Notification = new NotificationBar();
            // 
            // TextEditor
            // 
            this.Editor.Dock = System.Windows.Forms.DockStyle.Fill;
            this.Editor.IsReadOnly = false;
            this.Editor.Location = new System.Drawing.Point(0, 24);
            this.Editor.Name = "TextEditor";
            this.Editor.ShowSpaces = true;
            this.Editor.ShowTabs = true;
            this.Editor.Size = new System.Drawing.Size(706, 496);
            this.Editor.TabIndex = 0;
            
            //this.Editor.TextEditorProperties.ShowEOLMarker = true;
            this.Editor.TextEditorProperties.MouseWheelTextZoom = true;
            this.Editor.TextEditorProperties.EnableFolding = true;
            this.Editor.TextEditorProperties.ConvertTabsToSpaces = true;
            this.Editor.TextEditorProperties.AutoInsertCurlyBracket = true;

            Editor.TextChanged += Editor_TextChanged;
            // 
            // Notification
            // 
            this.Notification.BackColor = System.Drawing.SystemColors.Info;
            this.Notification.Dock = System.Windows.Forms.DockStyle.Top;
            this.Notification.Image = global::SimplePad.Properties.Resources.arrow_refresh;
            this.Notification.Location = new System.Drawing.Point(0, 0);
            this.Notification.Name = "Notification";
            this.Notification.PlaySoundWhenShown = true;
            this.Notification.Size = new System.Drawing.Size(706, 24);
            this.Notification.TabIndex = 1;
            this.Notification.Text = "Файл обновился! Хотите обновить?";
            this.Notification.Visible = false;
            this.Controls.Add(this.Editor);
            this.Controls.Add(this.Notification);

            OpenFile(fileName);
            OnStatusUpdate += TextEditor_OnStatusUpdate;
        }

        void TextEditor_OnStatusUpdate()
        {
            //Image = getIcon();
        }

        void Editor_TextChanged(object sender, EventArgs e)
        {
            if(TextHash!=Editor.Text.GetHashCode())
                Dirty = true;
            else
                Dirty = false;
            if (OnStatusUpdate != null)
            {
                OnStatusUpdate();
            }
        }

        public string FileName { get; protected set; }

        public FileInfo FileInfo { get; protected set; }

        public bool LockForEdit { get; protected set; }

        public bool Dirty { get; protected set; }

        public Image getIcon()
        {
            if (LockForEdit)
            {
                return Properties.Resources.bullet_black;
            }
            if (Dirty)
            {
                return Properties.Resources.bullet_red;
            }
            return Properties.Resources.bullet_blue;
        }

        public void OpenFile(string fileName)
        {
            FileName = fileName;
            FileInfo = new FileInfo(fileName);
            //Title = FileInfo.Name;
            LockForEdit = FileInfo.IsReadOnly;
            Editor.IsReadOnly = LockForEdit;
            //Image = getIcon();
            Editor.LoadFile(fileName);
            Dirty = false;
            TextHash = Editor.Text.GetHashCode();
        }

        public void toLowerCase()
        {
            if (Editor.ActiveTextAreaControl.Document.ReadOnly)
            {
                return;
            }
            if (Editor.ActiveTextAreaControl.SelectionManager.HasSomethingSelected)
            {
                string text = Editor.ActiveTextAreaControl.SelectionManager.SelectedText.ToLower();
                // Remove text
                Editor.ActiveTextAreaControl.TextArea.BeginUpdate();
                Editor.ActiveTextAreaControl.TextArea.Caret.Position = Editor.ActiveTextAreaControl.TextArea.SelectionManager.SelectionCollection[0].StartPosition;
                Editor.ActiveTextAreaControl.TextArea.SelectionManager.RemoveSelectedText();
                Editor.ActiveTextAreaControl.TextArea.InsertString(text);
                Editor.ActiveTextAreaControl.TextArea.EndUpdate();
            }
        }

        public void toUperCase()
        {
            if (Editor.ActiveTextAreaControl.Document.ReadOnly)
            {
                return;
            }
            if (Editor.ActiveTextAreaControl.SelectionManager.HasSomethingSelected)
            {
                string text = Editor.ActiveTextAreaControl.SelectionManager.SelectedText.ToUpper();
                // Remove text
                Editor.ActiveTextAreaControl.TextArea.BeginUpdate();
                Editor.ActiveTextAreaControl.TextArea.Caret.Position = Editor.ActiveTextAreaControl.TextArea.SelectionManager.SelectionCollection[0].StartPosition;
                Editor.ActiveTextAreaControl.TextArea.SelectionManager.RemoveSelectedText();
                Editor.ActiveTextAreaControl.TextArea.InsertString(text);
                Editor.ActiveTextAreaControl.TextArea.EndUpdate();                
            }
        }

        public static void save(TextEditor Editor, string FileName)
        {
            StreamWriter sw = new StreamWriter(FileName);
            sw.Write(Editor.Editor.ActiveTextAreaControl.Document.TextContent);
            sw.Close();
            Editor.Dirty = false;
            Editor.TextEditor_OnStatusUpdate();
        }
    }
}
